package com.greate.community.service.master;

import com.greate.community.dao.admin.RBACMapper;
import com.greate.community.entity.Permission;
import com.greate.community.entity.Role;
import com.greate.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RBACService {
    @Autowired
    RBACMapper rbacMapper;
    /**
     * 获得所有角色信息
     * @return
     */
    public List<Role> selectAllRoles(){
        return rbacMapper.selectAllRoles();
    }

    /**
     * 添加角色
     * @param role 角色
     * @param permissions 权限
     * @return
     */
    @Transactional
    public String addRole(Role role, List<Integer> permissions) {
        //添加角色
        rbacMapper.addRole(role.getRoleName(),role.getRoleDesc());
        //通过用户名查询角色 获得角色id
        int roleId = rbacMapper.selectRoleIdByName(role.getRoleName());
        //为角色添加权限
        rbacMapper.insertRolePermissions(roleId,permissions);
        return CommunityUtil.getJSONString(200,"角色创建成功");
    }

    /**
     * 更新角色信息
     * @param role
     * @param permissions
     * @return
     */
    @Transactional
    public String updateRole(Role role, List<Integer> permissions) {

        //修改role表信息
        rbacMapper.updateRoleInfo(role);
        //删除原先的权限
        rbacMapper.deleteRolePermission(role.getId());
        //添加新权限
        rbacMapper.insertRolePermissions(role.getId(),permissions);
        return CommunityUtil.getJSONString(200,"角色修改成功");

    }

    /**
     * 删除角色 这里应该是删除角色的状态 我这里偷个懒 直接删了
     * 删除role和role_permission表中对应的数据
     * @param roleId
     * @return
     */
    public String deleteRole(int roleId) {
        //删除role
        rbacMapper.deleteRoleById(roleId);
        //删除role_permission
        rbacMapper.deleteRolePermission(roleId);
        return CommunityUtil.getJSONString(200,"角色删除成功");
    }

    /**
     * 添加权限
     * @param permission
     * @return
     */
    public String addPermission(Permission permission) {
        rbacMapper.addPermission(permission.getPermName(),permission.getPermTag(),permission.getUrl());
        return CommunityUtil.getJSONString(200,"权限创建成功");
    }

    /**
     * 更新权限
     * @param permission
     * @return
     */
    public String updatePermission(Permission permission) {
        rbacMapper.updatePermission(permission);
        return CommunityUtil.getJSONString(200,"权限更新成功");
    }

    /**
     * 删除权限  应该用伪删除 我这里就不用了
     * 需要删除权限表 及其角色权限表对应的信息
     * @param permissionId
     * @return
     */
    public String deletePermission(int permissionId) {
        rbacMapper.deletePermissionById(permissionId);
        rbacMapper.deleteRolePermissionByPermissionId(permissionId);
        return CommunityUtil.getJSONString(200,"权限删除成功");
    }

    /**
     * 获得所有权限
     * @return
     */
    public List<Permission> getAllPermission() {
       return rbacMapper.getAllPermission();
    }

    /**
     *获得角色对应的权限id
     * @param roleId
     * @return
     */
    public List<Integer> selectPermissionByRoleId(int roleId) {

        return rbacMapper.selectPermissionIdsByRoleId(roleId);
    }

    /**
     * 添加角色的默认权限
     * @param permissionIds
     * @return
     */
    @Transactional
    public String setDefaultRolePermission(List<Integer> permissionIds) {
        //清除所有权限
        rbacMapper.deleteAllDefaultPermission();
        //插入新权限
        rbacMapper.setDefaultRolePermission(permissionIds);

        return  CommunityUtil.getJSONString(200,"角色默认权限设置成功");
    }

    /**
     * 获得角色的默认权限
     * @return
     */
    public List<Integer> getDefaultRolePermission() {
        return rbacMapper.getAllDefaultRolePermission();
    }

    public List<Permission> getPermissionByUserId(int userId){
        return rbacMapper.getPermissionByUserId(userId);
    };
}
